import pandas as pd
import pyecharts.options as opts
from pandas import DataFrame, Series
from pyecharts.charts import Line


class EchartView(object):
    def __init__(self, rank, mem, oom):
        self.rank: DataFrame = rank
        self.mem: DataFrame = mem
        self.rank_line = Line(init_opts=opts.InitOpts(width='1800px', height='768px'))
        self.oom: Series = oom
        self.data: DataFrame = pd.concat([self.rank, self.mem, self.oom], sort=True, axis=1)
        self.data.index = pd.to_datetime(self.data.index, format='%y%m%d%H%M%S')

    def save(self, path):
        self.data.to_csv(path, sep=',')

    def show(self, path):
        x_data = self.data.index.tolist()
        self.rank_line.add_xaxis(xaxis_data=x_data)

        # cat /sys/module/lowmemorykiller/parameters/minfree
        # cat /sys/module/lowmemorykiller/parameters/adj
        # cat /proc/2705/oom_score_adj
        # 0,100,200,300,900,906
        minfree = [14746, 18432, 22118, 25805, 40000, 55000]
        line_data = [opts.MarkLineItem(y=x * 4, symbol='none') for x in minfree]
        # point_data = [opts.MarkPointItem(coord=['190722131842', 9768], value=26416, name='name', symbol='roundRect',
        #                                  symbol_size=[50, 20])]

        for index in self.data.columns:
            y_data = self.data[index].tolist()
            if index == 'free':
                self.rank_line.add_yaxis(series_name=index,
                                         y_axis=y_data,
                                         is_selected=True,
                                         is_connect_nones=True,
                                         label_opts=opts.LabelOpts(is_show=False),
                                         is_smooth=True)
            elif index == 'LowMemoryKiller':
                self.rank_line.add_yaxis(series_name=index,
                                         y_axis=y_data,
                                         is_selected=True,
                                         is_connect_nones=True,
                                         # markpoint_opts=opts.MarkPointOpts(data=point_data),
                                         markline_opts=opts.MarkLineOpts(data=line_data),
                                         label_opts=opts.LabelOpts(is_show=False),
                                         linestyle_opts=opts.LineStyleOpts(color="green", width=3, type_="dashed"),
                                         itemstyle_opts=opts.ItemStyleOpts(border_width=10, border_color="red",
                                                                           color="blue"),
                                         is_smooth=False)
            elif 'dashcam' in index:
                self.rank_line.add_yaxis(series_name=index,
                                         y_axis=y_data,
                                         is_selected=True,
                                         is_connect_nones=True,
                                         label_opts=opts.LabelOpts(is_show=False),
                                         is_smooth=True)
            else:
                self.rank_line.add_yaxis(series_name=index,
                                         y_axis=y_data,
                                         is_selected=False,
                                         is_connect_nones=True,
                                         label_opts=opts.LabelOpts(is_show=False),
                                         is_smooth=True)

        self.rank_line.set_global_opts(
            # title_opts=opts.TitleOpts(title="memrank"),
            tooltip_opts=opts.TooltipOpts(
                trigger="axis",
                axis_pointer_type="cross"),
            toolbox_opts=opts.ToolboxOpts(),
            legend_opts=opts.LegendOpts(
                type_='scroll',
                orient='vertical',
                pos_right='0'),
            datazoom_opts=opts.DataZoomOpts(),
            # visualmap_opts=opts.VisualMapOpts(),
            yaxis_opts=opts.AxisOpts(
                type_="value",
                axistick_opts=opts.AxisTickOpts(is_show=True),
                splitline_opts=opts.SplitLineOpts(is_show=True),
            ),
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
        )
        self.rank_line.render(path)
